﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;

namespace QunYing.Web.market
{
    public partial class analysis : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Repeater rp = ((Repeater)Master.FindControl("NavRepeater"));
                rp.DataSource = NavData();
                rp.DataBind();
            }
        }
        /// <summary>
        /// 快捷导航数据
        /// </summary>
        private DataTable NavData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("url");
            dt.Rows.Add("统计分析", "");
            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager1.RegisterAsyncPostBackControl(btnAnalysis);
            ScriptManager1.RegisterAsyncPostBackControl(btnQuery);
            if (!IsPostBack)
            {
                txtStartDate1.Text = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                txtStartDate2.Text = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                txtEndDate1.Text = DateTime.Now.ToString("yyyy-MM-dd");
                txtEndDate2.Text = DateTime.Now.ToString("yyyy-MM-dd");
                ddlBind();
                GVBind();
            }
            DrawChart();
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void GVBind()
        {
            string userName = ((ArrayList)Session["userInfo"])[0].ToString();
            BLL.DeviceRealTimeInfo info = new BLL.DeviceRealTimeInfo();
            DateTime endDate = Convert.ToDateTime(txtEndDate2.Text.Trim());
            DateTime startDate = Convert.ToDateTime(txtStartDate2.Text.Trim());
            DataTable dt = info.QueryWarn(userName, ddlDevice2.SelectedValue, startDate, endDate);
            int intCount = dt.Rows.Count;
            bool b = true;
            if (intCount < 5)
            {
                b = false;
                for (int i = 0; i < 5 - intCount; i++)
                {
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                }

            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
            if (!b)
            {
                for (int i = intCount; i < GridView1.Rows.Count; i++)
                {
                    LinkButton btn = (LinkButton)GridView1.Rows[i].FindControl("btnDetail");
                    btn.Visible = false;
                }
            }

        }
        protected void btnDetail_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt16((sender as LinkButton).CommandArgument);
            string strIcsa = this.GridView1.DataKeys[id - 1].Values[1].ToString();//ICSA编号
            string strTime = this.GridView1.DataKeys[id - 1].Values[2].ToString();//告警时间
            Session["icsa"] = strIcsa;
            Session["time"] = Convert.ToDateTime(strTime).ToString("yyyy-MM-dd HH:mm:ss");
            Response.Redirect("~/water.aspx");
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i <= GridView1.Rows.Count; i++)
                {
                    if (e.Row.Cells[4].Text == "正报警")
                    {
                        e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
        }

        /// <summary>
        /// 绑定下拉框
        /// </summary>
        private void ddlBind()
        {
            string userName = ((ArrayList)Session["userInfo"])[0].ToString();
            BLL.DeviceInfo info = new BLL.DeviceInfo();
            DataTable dt = info.QueryDevice(userName);
            ddlDevice.DataSource = dt;
            ddlDevice.DataTextField = "名称";
            ddlDevice.DataValueField = "ICSA编号";
            ddlDevice.DataBind();

            ddlDevice2.DataSource = dt;
            ddlDevice2.DataTextField = "名称";
            ddlDevice2.DataValueField = "ICSA编号";
            ddlDevice2.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            GVBind();
            UpdatePanel2.Update();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GVBind();
        }
        protected void btnAnalysis_Click(object sender, EventArgs e)
        {
            DrawChart();
            UpdatePanel1.Update();
        }
        /// <summary>
        /// 画图表
        /// </summary>
        private void DrawChart()
        {
            Chart1.Series["Series1"].Points.Clear();
            DateTime endDate = Convert.ToDateTime(txtEndDate1.Text.Trim());
            DateTime startDate = Convert.ToDateTime(txtStartDate1.Text.Trim());
            BLL.DeviceRealTimeInfo info = new BLL.DeviceRealTimeInfo();
            DataTable dt = info.GetChartData(ddlDevice.SelectedValue, startDate, endDate);
            Chart1.DataSource = dt;
            Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 0;
            Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 0;
            Chart1.Series["Series1"].IsValueShownAsLabel = true;
            Chart1.Series["Series1"].MarkerStyle = MarkerStyle.Circle;
            Chart1.Series["Series1"].ToolTip = ddlType.SelectedValue + ":" + "#VAL";
            //Chart1.Series["Series1"].XValueMember ="数据接收时间";
            //Chart1.Series["Series1"].YValueMembers = ddlType.SelectedValue;
            for (int i = dt.Rows.Count - 1; i > -1; i--)
            {
                Chart1.Series["Series1"].Points.AddXY(Convert.ToDateTime(dt.Rows[i]["数据接收时间"]).ToString("yyyy-MM-dd"), dt.Rows[i][ddlType.SelectedValue].ToString());
            }

        }
    }
}
